import { observable, action, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import Apis from '../apis/car';
import ListBaseStore from './ListBaseStore';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 车辆查询Store
 */
export default class UseCarWorkflowStore extends ListBaseStore {
  constructor() {
    super();
    this.title = getLabel(126428, '用车流程设置') + getLabel(82, '新建');
  }

	@observable conditioninfo =[];
	@observable cube_isinit = false;
	@observable workflowRight = true;
	@observable detachable = true;

	@observable subcompanyid = 0;
	@observable subcompanySpan = '';
	@observable isdetach = false;

	@action initUseCarWorkflowSetCreateForm = (param) => {
	  Apis.getCreateUseCarWorkflowSet().then(
	    (result) => {
	      this.workflowRight = result.workflowRight;
	      this.detachable = result.detachable;
	      this.conditioninfo = result.conditioninfo;
	      this.form = new WeaForm();
	      this.form.initFormFields(result.conditioninfo);
	      if (!param || param.subCompanyId == '0') {
	        return;
					 }
					 this.subcompanyid = param.subCompanyId;
					 this.subcompanySpan = param.subCompanyName;
					 this.isdetach = param.subCompanyId != '0';
					  this.form.updateFields({
	        subcompanyid: {
						  value: param.subCompanyId,
						  valueSpan: param.subCompanyName,
						  valueObj: [
	            {
							  id: param.subCompanyId,
							  name: param.subCompanyName,
	            },
						  ],
	        },
					  });
	    },
	  );
	}

	@observable operation='';
	@observable id='';
	@observable workflowid='';
	@observable workflowData={};

	@action doSubmit = (param, callBack) => {
	  let resultParams = this.getFormParams();
	  resultParams = { ...resultParams, operation: this.operation, id: this.id, workflowid: this.workflowid };
	  Apis.submitCreateUseCarWorkflowSet(resultParams).then(
	    (result) => {
	      if (callBack) {
	        callBack(result);
	      }
	    },
	  );
	}

	@observable createDialogVisible = false;
	@observable addDialogVisible = false;
	@observable editDialogVisible = false;

	@observable addconditioninfo =[];
	@observable carInfoRelateModeconditioninfo =[];


	@observable addform = new WeaForm(); // 表单
	@action getAddUseCarWorkflowSetInitData = (param) => {
	  Apis.getAddUseCarWorkflowSetInitData(param).then(
	    (result) => {
	      this.addconditioninfo = result.conditioninfo;
	      this.addform = new WeaForm();
	      this.addform.initFormFields(this.addconditioninfo);
	    },
	  );
	}

	@observable carrelatemodeInfoform = new WeaForm(); // 表单
	@action getEditUseCarWorkflowSetInitData = (param) => {
	  Apis.getEditUseCarWorkflowSetInitData(param).then(
	    (result) => {
	      this.addconditioninfo = result.conditioninfo;
	      this.carInfoRelateModeconditioninfo = result.carInfoRelateModeconditioninfo;
	      this.addform = new WeaForm();
	      this.addform.initFormFields(toJS(this.addconditioninfo));
	      this.carrelatemodeInfoform = new WeaForm();
	      this.carrelatemodeInfoform.initFormFields(this.carInfoRelateModeconditioninfo);
	    },
	  );
	}

	@observable carbasicid=0;
	@action doSubmitByAdd = (param, callBack) => {
	  let resultParams = this.addform.getFormParams();

	  resultParams = { ...resultParams, ...param, operation: this.operation };
	  Apis.submitCreateUseCarWorkflowSet(resultParams).then(
	    (result) => {
	      if (this.addform.initFormFields) {
	        this.addform.reset();
	      }
	      // this.resultJson = result;
	      if (resultParams.operation == 'createToDetails2') {
	        this.addDialogVisible = false;
	        this.carbasicid = result.carbasicid;
	        this.editDialogVisible = true;
	      } else {
	        this.addDialogVisible = false;
	      }
	      if (callBack) {
	        callBack();
	      }
	    },
	  );
	}
	@observable resultJson ;
	@action workflowidChange = (param, callBack) => {
	  Apis.submitCreateUseCarWorkflowSet(param).then(
	    (result) => {
	      const dataJson = result.resultData;
	      const obj = {};
	      if (dataJson && dataJson.flag == 0) {
	        obj.formid = { value: dataJson.formid, valueSpan: dataJson.formname, valueObj: [{ id: dataJson.formid, name: dataJson.formname }] };
	        obj.typeid = { value: dataJson.workflowtype, valueSpan: '', valueObj: [{ id: dataJson.workflowtype, name: '' }] };
	      } else if (dataJson && dataJson.flag == 1) {
	        message.error(getLabel(128243, '该流程已被添加，如需重新添加，请先删除已有添加数据。'));
	        obj.formid = { value: '', valueSpan: '', valueObj: [{ id: '', name: '' }] };
	        obj.typeid = { value: '', valueSpan: '', valueObj: [{ id: '', name: '' }] };
	        obj.workflowid = { value: '', valueSpan: '', valueObj: [{ id: '', name: '' }] };
	      } else {
	        obj.formid = { value: '', valueSpan: '', valueObj: [{ id: '', name: '' }] };
	        obj.typeid = { value: '', valueSpan: '', valueObj: [{ id: '', name: '' }] };
	      }
	      this.workflowData = obj;
	      if (callBack) {
	        callBack();
	      }
	    },
	  );
	}

	@action doSubmitByEdit = (param, callBack) => {
	  let resultParams = this.addform.getFormParams();
	  resultParams = { ...resultParams, ...param, operation: this.operation };
	  Apis.submitCreateUseCarWorkflowSet(resultParams).then(
	    (result) => {
	      if (callBack) {
	        callBack();
	      }
	    },
	  );
	}

	@action doSubmitByRelateMode = (param, callBack) => {
	  let resultParams = this.carrelatemodeInfoform.getFormParams();
	  resultParams = { ...resultParams, ...param, operation: this.operation, workflowid: this.workflowid };
	  Apis.submitCreateUseCarWorkflowSet(resultParams).then(
	    (result) => {
	      if (callBack) {
	        callBack();
	      }
	    },
	  );
	}
	@action
	showEditForm=() => {
	  console.log('subCompanyIdhgm:', this.subcompanyid, this.subcompanySpan, this.isdetach);
	  window.weaCubeEngine.store.EditFormStore.isdetach = this.isdetach;
	  window.weaCubeEngine.store.EditFormStore.detachMap.id = this.subcompanyid;
	  window.weaCubeEngine.store.EditFormStore.detachMap.name = this.subcompanySpan;
	  window.weaCubeEngine.store.EditFormStore.detachMap.operatelevel = 1;
	  window.weaCubeEngine.store.EditFormStore.carflag = '1';
	  window.weaCubeEngine.store.EditFormStore.init('', 0);
	  window.weaCubeEngine.store.EditFormStore.afterAddCallBack = (formid) => {
	    const formname = window.weaCubeEngine.store.EditFormStore.formname;
	    const obj = {};
	    obj.formid = { value: formid, valueSpan: formname, valueObj: [{ id: formid, name: formname }] };
	    this.form.updateFields(obj, false);
	    window.weaCubeEngine.store.EditFormStore.form.clear();
	    window.weaCubeEngine.store.EditFormStore.isdetach = false;
	    window.weaCubeEngine.store.EditFormStore.detachMap.id = '';
	    window.weaCubeEngine.store.EditFormStore.detachMap.name = '';
	  };
	  window.weaCubeEngine.store.EditFormStore.visible = false;
	}
}
